 class ListNode {
     int val;
     ListNode next;
     ListNode() {}
     ListNode(int val) { this.val = val; }
     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}

public class MergeTwoLists {
    public ListNode mergeTwoLists(ListNode headA, ListNode headB) {
        ListNode Nhead=new ListNode();
        ListNode pre=Nhead;
        while(headA!=null&&headB!=null){
            if(headA.val<headB.val){
                pre.next=headA;
                pre=headA;
                headA=headA.next;
            }else{
                pre.next=headB;
                pre=headB;
                headB=headB.next;
            }

        }
        if(headA==null){
            pre.next=headB;
        }else{
            pre.next=headA;
        }
        return Nhead.next;
    }